import { Component, OnInit } from '@angular/core';
import { AxiosService } from '../../axios.service';


@Component({
  selector: 'app-manage-ruku',
  templateUrl: './manage-ruku.component.html',
  styleUrls: ['./manage-ruku.component.scss']
})
export class ManageRukuComponent implements OnInit {
  listOfData;
  editCache = {};
  isVisible;
  PR_NO; // PR_NO
  PO_NO; // PO_NO
  SN; // 序號
  material_no; // 料號
  quatity; // 数量
  price; // 含税单价
  manufacturer; // 厂商
  currency; // 币别
  check_no; // 卡口核放单号
  purchasing_reason; // 采购原因
  liaohao = ''; // 查询输入的料号
  subscribeScoll: any;
  flag = false;
  count = 50;
  index = 0;
  warp;
  id;
  constructor(public axios: AxiosService) { }

  ngOnInit(): void {
    this.warp = document.getElementById("warp");
    this.getData();
  }

  // 查询数据
  getData() {
    console.log(this.liaohao)
    if (this.liaohao == '') {
      this.axios.axiosPost('QueryWarehouse', { offset: 0, count: 50 })
        .then((data) => {
          if (data) {
            this.listOfData = data['order'];
            for (let i = 0; i < this.listOfData.length; i++) {
              this.editCache[i] = { edit: true, isdel: true, data: this.listOfData[i] };
            }
            console.log(this.editCache)
          }
        })
        .catch((err) => {
          console.log(err);
        })
    } else {
      this.axios.axiosPost('QueryWarehouse', { offset: 0, count: 50, keyword: this.liaohao })
        .then((data) => {
          if (data) {
            this.listOfData = data['order'];
            for (let i = 0; i < this.listOfData.length; i++) {
              this.editCache[i] = { edit: true, isdel: true, data: this.listOfData[i] };
            }
            console.log(this.editCache)
          }
        })
        .catch((err) => {
          console.log(err);
        })
    }
  }
  // 滚动条滚到底部继续加载数据
  againGet() {
    console.log("1")
    var scrollTop = this.warp.scrollTop;
    // 是可视区的高度
    var windowHeight = this.warp.clientHeight;
    // 是滚动条的总高度（当前可滚动的页面的总高度）
    var scrollHeight = this.warp.scrollHeight;
    console.log(scrollTop, windowHeight, scrollHeight)
    if (scrollTop + windowHeight >= scrollHeight && scrollHeight) {
      if (this.index === 0) {
        this.count += 50;
        if (this.liaohao == '') {
          this.axios.axiosPost("QueryWarehouse", { offset: 0, count: this.count }).then((data) => {
            if (data) {
              this.listOfData = data["order"];
              for (let i = 0; i < this.listOfData.length; i++) {
                this.editCache[i] = { edit: true, isdel: true, data: this.listOfData[i] };
              }
            }
          })
        } else {
          this.axios.axiosPost("QueryWarehouse", { offset: 0, count: this.count, keyword: this.liaohao }).then((data) => {
            if (data) {
              this.listOfData = data["order"];
              for (let i = 0; i < this.listOfData.length; i++) {
                this.editCache[i] = { edit: true, isdel: true, data: this.listOfData[i] };
              }
            }
          })
        }
      }
    }
  }

  // 修改信息
  startEdit(key: number): void {
    console.log(key)
    console.log(this.editCache[key])
    console.log(this.editCache[key]['data'].material_no)
    this.editCache[key].isdel = false;
    this.editCache[key].edit = false;
    this.id = this.editCache[key]['data'].id;
    console.log(this.id)
  }
  // 保存修改
  saveEdit(key: number): void {
    console.log(this.editCache[key]['data'].material_no)
    // debugger
    let bpMsg = []; // 备品信息表的料号
    let chsMsg = []; // 厂商信息表的厂别
    this.axios.axiosPost('QueryBaseMaterial')
      .then((data) => {
        if (data) {
          for (let i = 0; i < data['order'].length; i++) {
            bpMsg.push(data['order'][i].material_no);
          }
          console.log(bpMsg);
          this.axios.axiosPost('QueryFirmInfo')
            .then((data) => {
              if (data) {
                for (let i = 0; i < data['order'].length; i++) {
                  chsMsg.push(data['order'][i].fullname);
                }
                console.log(chsMsg);
                debugger
                if (bpMsg.includes(this.editCache[key]['data'].material_no) && chsMsg.includes(this.editCache[key]['data'].manufacturer)) {
                  this.axios.axiosPost('UpdateWarehouse', {
                    id: this.id,
                    PR_NO: this.editCache[key]['data'].PR_NO,
                    PO_NO: this.editCache[key]['data'].PO_NO,
                    SN: this.editCache[key]['data'].SN,
                    material_no: this.editCache[key]['data'].material_no,
                    quatity: this.editCache[key]['data'].quatity,
                    price: this.editCache[key]['data'].price,
                    manufacturer: this.editCache[key]['data'].manufacturer,
                    currency: this.editCache[key]['data'].currency,
                    check_no: this.editCache[key]['data'].check_no,
                    purchasing_reason: this.editCache[key]['data'].purchasing_reason
                  })
                    .then((data) => {
                      if (data['result'] === 'ok') {
                        this.editCache[key].isdel = true;
                        this.editCache[key].edit = true;
                        alert('OK')
                        console.log(this.editCache[key]['data'].reason)
                        console.log(data);
                        this.getData();
                      }
                    })
                    .catch((err) => {
                      console.log(err);
                    })
                } else {
                  alert('料號或廠商不存在');
                  this.editCache[key].isdel = false;
                  this.editCache[key].edit = false;
                  // return;
                }
              }
            })
        }
      })
  }
  // 撤销修改
  cancelEdit(key: number): void {
    // debugger
    // const index = this.listOfData.findIndex((item, index) => index === key);
    // this.editCache[key] = {
    //   data: { ...this.listOfData },
    //   edit: true,
    //   isdel: true
    // };
    this.getData();
  }

  // 删除
  showModal(item): void {
    this.isVisible = true;
    this.material_no = item.material_no;
    this.id = item.id
  }
  handleOk() {
    this.isVisible = false;
    debugger
    this.axios.axiosPost('DeleteWarehouse', { id: this.id })
      .then((data) => {
        console.log(data)
        if (data['result'] === 'ok') {
          this.getData();
        }
      })
      .catch((err) => {
        console.log(err);
      })
  }
  handleCancel(): void {
    this.isVisible = false;
  }

}
